<?php
/**
 * User: thanhdx
 * Date: 1/18/12
 * Time: 4:40 PM
 */
class UserController extends Controller
{
    public $layout = "column1";

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions'=>array('profile','edit','changepass','delclass'),
                //'roles'=>array('register'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }
    public function actionProfile($username)
    {
        
        $model = User::model()->findByAttributes(array('username'=>$username));
        $students = $model->students();
        $gClass = array();
//        $gClass = null;
        if($students){
            foreach($students as $st){
                //var_dump($st->guitarclass);
                $gClass = array_merge($gClass,$st->guitarclass);
            }
            /*if($clsId){
                $criteria = new CDbCriteria(array('condition'=>'id IN ('.implode(',',$clsId).')'));
                $gClass = GuitarClass::model()->findAll($criteria);
            }*/
        }
        $this->render("profile", array('model' => $model,'gclasses'=>$gClass));

    }


    public function actionEdit()
    {
        /**
         * @var $model User
         */
        $model = User::model()->findByPk(Yii::app()->user->id);

        if (isset($_POST['ajax']) && $_POST['ajax'] === 'user-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        if (isset($_POST['User'])) {
            $model->display_name = $_POST['User']['display_name'];
            $model->sex = $_POST['User']['sex'];
            $model->tel = $_POST['User']['tel'];
            $model->address = $_POST['User']['address'];
            $model->home_page = $_POST['User']['home_page'];
            $arr = explode("/", $_POST['User']['birthday']);
            if ($arr) {
                $model->birthday = mktime(0, 0, 0, $arr[1], $arr[0], $arr[2]);
            }

            if ($model->save()) {
                $this->redirect($this->createUrl('profile'));
            }
        }

        $this->render("edit", array('model' => $model));
    }

    public function actionChangepass()
    {
        /**
         * @var $model User
         */
        $model = new ChangePassForm();
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'user-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        if(isset($_POST['ChangePassForm'])){
            $model->attributes = $_POST['ChangePassForm'];
            if($model->validate()&& $model->save()){
                //$this->redirect($this->createUrl(''))
                Yii::app()->user->setFlash('changepass','Bạn đã thay đổi mật khẩu thành công');
            }
        }

        $this->render("changepass", array('model' => $model));
    }

    public function actionDelclass($id){
        $cls_st = Clstudent::model()->findByAttributes(array('class_id'=>$id));
        $ok = false;
        if($cls_st->student){
            $cls_st->student->delete();
            $cls_st->delete();
            $ok = true;
            $user = User::model()->findByPk(Yii::app()->user->id);
            $user->training_id = null;
            $user->update(array('training_id'));
        }

        header('Content-Type: application/json');
        echo json_encode(array('success'=>$ok));
//        $db = Yii::app()->db;

    }

    private function getExtension($file)
    {
        $pos = strrpos($file, '.');
        if (!$pos) {
            return false;
        }
        $str = substr($file, $pos, strlen($file));
        return strtolower($str);
    }

    private function generatePw()
    {
        $chars = "abcdefghijkmnopqrstuvwxyz023456789";
        srand((double)microtime() * 1000000);
        $i = 0;
        $pass = '';

        while ($i <= 7) {
            $num = rand() % 33;
            $tmp = substr($chars, $num, 1);
            $pass = $pass . $tmp;
            $i++;
        }

        return $pass;
    }

}
